﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://wpcontrol.sourceforge.net/ for more.
//
// Copyright (c) 2014 nscrolldev
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WallpaperControl
{
    /// <summary>
    /// Represents a session that can change the wallpaper.
    /// </summary>
    class Session
    {
        /// <summary>
        /// Indicates whether the session is currently running.
        /// </summary>
        private bool _active;

        /// <summary>
        /// The active condition.
        /// </summary>
        private Condition _activeCondition;

        /// <summary>
        /// The unmodified condition.
        /// </summary>
        private Condition _oldCondition;

        /// <summary>
        /// Indicates whether the session has been changed.
        /// </summary>
        private bool _isChanged;

        /// <summary>
        /// Gets whether this session is active.
        /// </summary>
        internal bool IsActive
        {
            get
            {
                return this._active;
            }
            private set
            {
                this._active = value;
            }
        }

        /// <summary>
        /// Gets or sets the active condition.
        /// </summary>
        internal Condition ActiveCondition
        {
            get
            {
                return this._activeCondition;
            }
            set
            {
                if (value != null)
                {
                    this._activeCondition = value;
                    this.ConditionChanged = true;
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }

        /// <summary>
        /// Gets the unmodified condition.
        /// </summary>
        internal Condition OldCondition
        {
            get
            {
                return this._oldCondition;
            }
            private set
            {
                this._oldCondition = value;
            }
        }

        /// <summary>
        /// Determines whether the session has been modified.
        /// </summary>
        private bool ConditionChanged
        {
            get
            {
                return this._isChanged;
            }
            set
            {
                this._isChanged = value;
            }
        }

        /// <summary>
        /// Starts the session.
        /// </summary>
        /// <exception cref="InvalidSessionException">
        /// Thrown if the session can't be started because it is invalid.
        /// </exception>
        internal void Start()
        {
            if (!this.IsActive)
            {
                if (this.Validate())
                {
                    // Disallow condition changing by the user
                    Program.ControlPanel.AllowConditionChanging(false);
                    Program.ControlPanel.AllowSettingsChanging(false);
                    this.IsActive = true;
                    WallpaperManager.Instance.SetDefaultValues();
                    // Set first wallpaper in list
                    //WallpaperManager.Instance.SetWallpaper(0);
                    // Start checking and set next wallpaper
                    this.ActiveCondition.Check();
                }
                else
                {
                    throw new InvalidSessionException();
                }
            }
        }

        /// <summary>
        /// Stops the session.
        /// </summary>
        internal void Stop()
        {
            if (this.IsActive)
            {
                // If interval condition is set stop the timer
                if (this.ActiveCondition.Id == CondTimeInterval.CondId)
                {
                    this.ActiveCondition.StopSession();
                }
                // Stops time checking and keeps indication up-to-date
                this.IsActive = false;
                // Enable the user to change settings again
                Program.ControlPanel.AllowConditionChanging(true);
                Program.ControlPanel.AllowSettingsChanging(true);
            }
        }

        /// <summary>
        /// Validates the session.
        /// </summary>
        internal bool Validate()
        {
            // Field that indicates whether the session is valid.
            bool sessionValid;

            bool imagesInList = false;
            if (ImageList.Instance.Images.Count > 0)
            {
                imagesInList = true;
            }

            // Check if every file in the list exists.
            bool pathsValid = true;
            foreach (Wallpaper wallpaper in ImageList.Instance.Images)
            {
                if (!File.Exists(wallpaper.Path))
                {
                    pathsValid = false;
                }
            }

            // Check if every part of the session is valid.
            if (
                imagesInList &&
                pathsValid
               )
            {
                sessionValid = true;
            }
            else
            {
                sessionValid = false;
            }
            return sessionValid;
        }

        /// <summary>
        /// Restores the old condition.
        /// </summary>
        internal void RestoreCondition()
        {
            // Check that original condition is available
            if (this.OldCondition != null)
            {
                // Set image list to original list
                this.ActiveCondition = this.OldCondition;
                // Mark session as unchanged
                this.ConditionChanged = false;
            }
        }
    }
}
